from typing import Dict, Optional
import os
from .schemas import ResourceDescription


class ResourceManager:
    def __init__(self, base_path: str = None):
        self.base_path = base_path or os.getcwd()
        self._resources: Dict[str, ResourceDescription] = {}

    def add_resource(self, uri: str, description: str, content_type: str = "text/plain"):
        self._resources[uri] = ResourceDescription(
            uri=uri,
            description=description,
            content_type=content_type
        )

    def list_resources(self) -> Dict[str, ResourceDescription]:
        return self._resources

    def get_resource(self, uri: str) -> Optional[bytes]:
        if uri not in self._resources:
            return None

        # 简单实现：从文件系统读取
        full_path = os.path.join(self.base_path, uri)
        if not os.path.exists(full_path):
            return None

        with open(full_path, 'rb') as f:
            return f.read()